<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>tagonenterhandler_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_editor_plugins_tagonenterhandler_test.js.html">tagonenterhandler_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.editor.plugins.TagOnEnterHandlerTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.editor.plugins.TagOnEnterHandlerTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.dom&#39;);
<a name="line19"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line20"></a>goog.require(&#39;goog.dom.Range&#39;);
<a name="line21"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line22"></a>goog.require(&#39;goog.editor.BrowserFeature&#39;);
<a name="line23"></a>goog.require(&#39;goog.editor.Field&#39;);
<a name="line24"></a>goog.require(&#39;goog.editor.Plugin&#39;);
<a name="line25"></a>goog.require(&#39;goog.editor.plugins.TagOnEnterHandler&#39;);
<a name="line26"></a>goog.require(&#39;goog.events.KeyCodes&#39;);
<a name="line27"></a>goog.require(&#39;goog.string.Unicode&#39;);
<a name="line28"></a>goog.require(&#39;goog.testing.dom&#39;);
<a name="line29"></a>goog.require(&#39;goog.testing.editor.TestHelper&#39;);
<a name="line30"></a>goog.require(&#39;goog.testing.events&#39;);
<a name="line31"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line32"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line33"></a>
<a name="line34"></a>var savedHtml;
<a name="line35"></a>
<a name="line36"></a>var editor;
<a name="line37"></a>var field1;
<a name="line38"></a>
<a name="line39"></a>function setUp() {
<a name="line40"></a>  field1 = makeField(&#39;field1&#39;);
<a name="line41"></a>  field1.makeEditable();
<a name="line42"></a>}
<a name="line43"></a>
<a name="line44"></a>
<a name="line45"></a>/**
<a name="line46"></a> * Tests that deleting a BR that comes right before a block element works.
<a name="line47"></a> * @bug 1471096
<a name="line48"></a> */
<a name="line49"></a>function testDeleteBrBeforeBlock() {
<a name="line50"></a>  // This test only works on Gecko, because it&#39;s testing for manual deletion of
<a name="line51"></a>  // BR tags, which is done only for Gecko. For other browsers we fall through
<a name="line52"></a>  // and let the browser do the delete, which can only be tested with a robot
<a name="line53"></a>  // test (see javascript/apps/editor/tests/delete_br_robot.html).
<a name="line54"></a>  if (goog.userAgent.GECKO) {
<a name="line55"></a>
<a name="line56"></a>    field1.setHtml(false, &#39;one&lt;br&gt;&lt;br&gt;&lt;div&gt;two&lt;/div&gt;&#39;);
<a name="line57"></a>    var helper = new goog.testing.editor.TestHelper(field1.getElement());
<a name="line58"></a>    helper.select(field1.getElement(), 2); // Between the two BR&#39;s.
<a name="line59"></a>    goog.testing.events.fireKeySequence(field1.getElement(),
<a name="line60"></a>                                        goog.events.KeyCodes.DELETE);
<a name="line61"></a>    assertEquals(&#39;Should have deleted exactly one &lt;br&gt;&#39;,
<a name="line62"></a>                 &#39;one&lt;br&gt;&lt;div&gt;two&lt;/div&gt;&#39;,
<a name="line63"></a>                 field1.getElement().innerHTML);
<a name="line64"></a>
<a name="line65"></a>  } // End if GECKO
<a name="line66"></a>}
<a name="line67"></a>
<a name="line68"></a>
<a name="line69"></a>/**
<a name="line70"></a> * Tests that deleting a BR is working normally (that the workaround for the
<a name="line71"></a> * bug is not causing double deletes).
<a name="line72"></a> * @bug 1471096
<a name="line73"></a> */
<a name="line74"></a>function testDeleteBrNormal() {
<a name="line75"></a>  // This test only works on Gecko, because it&#39;s testing for manual deletion of
<a name="line76"></a>  // BR tags, which is done only for Gecko. For other browsers we fall through
<a name="line77"></a>  // and let the browser do the delete, which can only be tested with a robot
<a name="line78"></a>  // test (see javascript/apps/editor/tests/delete_br_robot.html).
<a name="line79"></a>  if (goog.userAgent.GECKO) {
<a name="line80"></a>
<a name="line81"></a>    field1.setHtml(false, &#39;one&lt;br&gt;&lt;br&gt;&lt;br&gt;two&#39;);
<a name="line82"></a>    var helper = new goog.testing.editor.TestHelper(field1.getElement());
<a name="line83"></a>    helper.select(field1.getElement(), 2); // Between the first and second BR&#39;s.
<a name="line84"></a>    field1.getElement().focus();
<a name="line85"></a>    goog.testing.events.fireKeySequence(field1.getElement(),
<a name="line86"></a>                                        goog.events.KeyCodes.DELETE);
<a name="line87"></a>    assertEquals(&#39;Should have deleted exactly one &lt;br&gt;&#39;,
<a name="line88"></a>                 &#39;one&lt;br&gt;&lt;br&gt;two&#39;,
<a name="line89"></a>                 field1.getElement().innerHTML);
<a name="line90"></a>
<a name="line91"></a>  } // End if GECKO
<a name="line92"></a>}
<a name="line93"></a>
<a name="line94"></a>
<a name="line95"></a>/**
<a name="line96"></a> * Regression test for http://b/1991234 . Tests that when you hit enter and it
<a name="line97"></a> * creates a blank line with whitespace and a BR, the cursor is placed in the
<a name="line98"></a> * whitespace text node instead of the BR, otherwise continuing to type will
<a name="line99"></a> * create adjacent text nodes, which causes browsers to mess up some
<a name="line100"></a> * execcommands. Fix is in a Gecko-only codepath, thus test runs only for Gecko.
<a name="line101"></a> * A full test for the entire sequence that reproed the bug is in
<a name="line102"></a> * javascript/apps/editor/tests/ponenter_robot.html .
<a name="line103"></a> */
<a name="line104"></a>function testEnterCreatesBlankLine() {
<a name="line105"></a>  if (goog.userAgent.GECKO) {
<a name="line106"></a>    field1.setHtml(false, &#39;&lt;p&gt;one &lt;br&gt;&lt;/p&gt;&#39;);
<a name="line107"></a>    var helper = new goog.testing.editor.TestHelper(field1.getElement());
<a name="line108"></a>    // Place caret after &#39;one&#39; but keeping a space and a BR as FF does.
<a name="line109"></a>    helper.select(&#39;one &#39;, 3);
<a name="line110"></a>    field1.getElement().focus();
<a name="line111"></a>    goog.testing.events.fireKeySequence(field1.getElement(),
<a name="line112"></a>                                        goog.events.KeyCodes.ENTER);
<a name="line113"></a>    var range = field1.getRange();
<a name="line114"></a>    assertFalse(&#39;Selection should not be in BR tag&#39;,
<a name="line115"></a>                range.getStartNode().nodeType == goog.dom.NodeType.ELEMENT &amp;&amp;
<a name="line116"></a>                range.getStartNode().tagName == goog.dom.TagName.BR);
<a name="line117"></a>    assertEquals(&#39;Selection should be in text node to avoid creating adjacent&#39; +
<a name="line118"></a>                 &#39; text nodes&#39;,
<a name="line119"></a>        goog.dom.NodeType.TEXT, range.getStartNode().nodeType);
<a name="line120"></a>    var rangeStartNode =
<a name="line121"></a>        goog.dom.Range.createFromNodeContents(range.getStartNode());
<a name="line122"></a>    assertHTMLEquals(&#39;The value of selected text node should be replaced with&#39; +
<a name="line123"></a>        &#39;&amp;nbsp;&#39;,
<a name="line124"></a>        &#39;&amp;nbsp;&#39;, rangeStartNode.getHtmlFragment());
<a name="line125"></a>  }
<a name="line126"></a>}
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>/**
<a name="line130"></a> * Regression test for http://b/3051179 . Tests that when you hit enter and it
<a name="line131"></a> * creates a blank line with a BR and the cursor is placed in P.
<a name="line132"></a> * Splitting DOM causes to make an empty text node. Then if the cursor is placed
<a name="line133"></a> * at the text node the cursor is shown at wrong location.
<a name="line134"></a> * Therefore this test checks that the cursor is not placed at an empty node.
<a name="line135"></a> * Fix is in a Gecko-only codepath, thus test runs only for Gecko.
<a name="line136"></a> */
<a name="line137"></a>function testEnterNormalizeNodes() {
<a name="line138"></a>  if (goog.userAgent.GECKO) {
<a name="line139"></a>    field1.setHtml(false, &#39;&lt;p&gt;one&lt;br&gt;&lt;/p&gt;&#39;);
<a name="line140"></a>    var helper = new goog.testing.editor.TestHelper(field1.getElement());
<a name="line141"></a>    // Place caret after &#39;one&#39; but keeping a BR as FF does.
<a name="line142"></a>    helper.select(&#39;one&#39;, 3);
<a name="line143"></a>    field1.getElement().focus();
<a name="line144"></a>    goog.testing.events.fireKeySequence(field1.getElement(),
<a name="line145"></a>                                        goog.events.KeyCodes.ENTER);
<a name="line146"></a>    var range = field1.getRange();
<a name="line147"></a>    assertTrue(&#39;Selection should be in P tag&#39;,
<a name="line148"></a>        range.getStartNode().nodeType == goog.dom.NodeType.ELEMENT &amp;&amp;
<a name="line149"></a>        range.getStartNode().tagName == goog.dom.TagName.P);
<a name="line150"></a>    assertTrue(&#39;Selection should be at the head and collapsed&#39;,
<a name="line151"></a>        range.getStartOffset() == 0 &amp;&amp; range.isCollapsed());
<a name="line152"></a>  }
<a name="line153"></a>}
<a name="line154"></a>
<a name="line155"></a>
<a name="line156"></a>/**
<a name="line157"></a> * Verifies
<a name="line158"></a> * goog.editor.plugins.TagOnEnterHandler.prototype.handleRegularEnterGecko_
<a name="line159"></a> * when we explicitly split anchor elements. This test runs only for Gecko
<a name="line160"></a> * since this is a Gecko-only codepath.
<a name="line161"></a> */
<a name="line162"></a>function testEnterAtBeginningOfLink() {
<a name="line163"></a>  if (goog.userAgent.GECKO) {
<a name="line164"></a>    field1.setHtml(false, &#39;&lt;a href=&quot;/&quot;&gt;b&lt;br&gt;&lt;/a&gt;&#39;);
<a name="line165"></a>    var helper = new goog.testing.editor.TestHelper(field1.getElement());
<a name="line166"></a>    field1.focusAndPlaceCursorAtStart();
<a name="line167"></a>    goog.testing.events.fireKeySequence(field1.getElement(),
<a name="line168"></a>                                        goog.events.KeyCodes.ENTER);
<a name="line169"></a>    helper.assertHtmlMatches(
<a name="line170"></a>        &#39;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;/&quot;&gt;b&lt;br&gt;&lt;/a&gt;&lt;/p&gt;&#39;);
<a name="line171"></a>  }
<a name="line172"></a>}
<a name="line173"></a>
<a name="line174"></a>
<a name="line175"></a>/**
<a name="line176"></a> * Verifies correct handling of pressing enter in an empty list item.
<a name="line177"></a> */
<a name="line178"></a>function testEnterInEmptyListItemInEmptyList() {
<a name="line179"></a>  if (goog.userAgent.GECKO) {
<a name="line180"></a>    field1.setHtml(false, &#39;&lt;ul&gt;&lt;li&gt;&amp;nbsp;&lt;/li&gt;&lt;/ul&gt;&#39;);
<a name="line181"></a>    var helper = new goog.testing.editor.TestHelper(field1.getElement());
<a name="line182"></a>    var li = field1.getElement().getElementsByTagName(goog.dom.TagName.LI)[0];
<a name="line183"></a>    helper.select(li.firstChild, 0);
<a name="line184"></a>    goog.testing.events.fireKeySequence(field1.getElement(),
<a name="line185"></a>                                        goog.events.KeyCodes.ENTER);
<a name="line186"></a>    helper.assertHtmlMatches(&#39;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&#39;);
<a name="line187"></a>  }
<a name="line188"></a>}
<a name="line189"></a>
<a name="line190"></a>
<a name="line191"></a>function testEnterInEmptyListItemAtBeginningOfList() {
<a name="line192"></a>  if (goog.userAgent.GECKO) {
<a name="line193"></a>    field1.setHtml(false,
<a name="line194"></a>        &#39;&lt;ul style=&quot;font-weight: bold&quot;&gt;&#39; +
<a name="line195"></a>            &#39;&lt;li&gt;&amp;nbsp;&lt;/li&gt;&#39; +
<a name="line196"></a>            &#39;&lt;li&gt;1&lt;/li&gt;&#39; +
<a name="line197"></a>            &#39;&lt;li&gt;2&lt;/li&gt;&#39; +
<a name="line198"></a>        &#39;&lt;/ul&gt;&#39;);
<a name="line199"></a>    var helper = new goog.testing.editor.TestHelper(field1.getElement());
<a name="line200"></a>    var li = field1.getElement().getElementsByTagName(goog.dom.TagName.LI)[0];
<a name="line201"></a>    helper.select(li.firstChild, 0);
<a name="line202"></a>    goog.testing.events.fireKeySequence(field1.getElement(),
<a name="line203"></a>                                        goog.events.KeyCodes.ENTER);
<a name="line204"></a>    helper.assertHtmlMatches(
<a name="line205"></a>        &#39;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;ul style=&quot;font-weight: bold&quot;&gt;&lt;li&gt;1&lt;/li&gt;&lt;li&gt;2&lt;/li&gt;&lt;/ul&gt;&#39;);
<a name="line206"></a>  }
<a name="line207"></a>}
<a name="line208"></a>
<a name="line209"></a>
<a name="line210"></a>function testEnterInEmptyListItemAtEndOfList() {
<a name="line211"></a>  if (goog.userAgent.GECKO) {
<a name="line212"></a>    field1.setHtml(false,
<a name="line213"></a>        &#39;&lt;ul style=&quot;font-weight: bold&quot;&gt;&#39; +
<a name="line214"></a>            &#39;&lt;li&gt;1&lt;/li&gt;&#39; +
<a name="line215"></a>            &#39;&lt;li&gt;2&lt;/li&gt;&#39; +
<a name="line216"></a>            &#39;&lt;li&gt;&amp;nbsp;&lt;/li&gt;&#39; +
<a name="line217"></a>        &#39;&lt;/ul&gt;&#39;);
<a name="line218"></a>    var helper = new goog.testing.editor.TestHelper(field1.getElement());
<a name="line219"></a>    var li = field1.getElement().getElementsByTagName(goog.dom.TagName.LI)[2];
<a name="line220"></a>    helper.select(li.firstChild, 0);
<a name="line221"></a>    goog.testing.events.fireKeySequence(field1.getElement(),
<a name="line222"></a>                                        goog.events.KeyCodes.ENTER);
<a name="line223"></a>    helper.assertHtmlMatches(
<a name="line224"></a>        &#39;&lt;ul style=&quot;font-weight: bold&quot;&gt;&lt;li&gt;1&lt;/li&gt;&lt;li&gt;2&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&#39;);
<a name="line225"></a>  }
<a name="line226"></a>}
<a name="line227"></a>
<a name="line228"></a>
<a name="line229"></a>function testEnterInEmptyListItemInMiddleOfList() {
<a name="line230"></a>  if (goog.userAgent.GECKO) {
<a name="line231"></a>    field1.setHtml(false,
<a name="line232"></a>        &#39;&lt;ul style=&quot;font-weight: bold&quot;&gt;&#39; +
<a name="line233"></a>            &#39;&lt;li&gt;1&lt;/li&gt;&#39; +
<a name="line234"></a>            &#39;&lt;li&gt;&amp;nbsp;&lt;/li&gt;&#39; +
<a name="line235"></a>            &#39;&lt;li&gt;2&lt;/li&gt;&#39; +
<a name="line236"></a>        &#39;&lt;/ul&gt;&#39;);
<a name="line237"></a>    var helper = new goog.testing.editor.TestHelper(field1.getElement());
<a name="line238"></a>    var li = field1.getElement().getElementsByTagName(goog.dom.TagName.LI)[1];
<a name="line239"></a>    helper.select(li.firstChild, 0);
<a name="line240"></a>    goog.testing.events.fireKeySequence(field1.getElement(),
<a name="line241"></a>                                        goog.events.KeyCodes.ENTER);
<a name="line242"></a>    helper.assertHtmlMatches(
<a name="line243"></a>        &#39;&lt;ul style=&quot;font-weight: bold&quot;&gt;&lt;li&gt;1&lt;/li&gt;&lt;/ul&gt;&#39; +
<a name="line244"></a>        &#39;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&#39; +
<a name="line245"></a>        &#39;&lt;ul style=&quot;font-weight: bold&quot;&gt;&lt;li&gt;2&lt;/li&gt;&lt;/ul&gt;&#39;);
<a name="line246"></a>  }
<a name="line247"></a>}
<a name="line248"></a>
<a name="line249"></a>
<a name="line250"></a>function testEnterInEmptyListItemInSublist() {
<a name="line251"></a>  if (goog.userAgent.GECKO) {
<a name="line252"></a>    field1.setHtml(false,
<a name="line253"></a>        &#39;&lt;ul&gt;&#39; +
<a name="line254"></a>        &#39;&lt;li&gt;A&lt;/li&gt;&#39; +
<a name="line255"></a>        &#39;&lt;ul style=&quot;font-weight: bold&quot;&gt;&#39; +
<a name="line256"></a>        &#39;&lt;li&gt;1&lt;/li&gt;&#39; +
<a name="line257"></a>        &#39;&lt;li&gt;&amp;nbsp;&lt;/li&gt;&#39; +
<a name="line258"></a>        &#39;&lt;li&gt;2&lt;/li&gt;&#39; +
<a name="line259"></a>        &#39;&lt;/ul&gt;&#39; +
<a name="line260"></a>        &#39;&lt;li&gt;B&lt;/li&gt;&#39; +
<a name="line261"></a>        &#39;&lt;/ul&gt;&#39;);
<a name="line262"></a>    var helper = new goog.testing.editor.TestHelper(field1.getElement());
<a name="line263"></a>    var li = field1.getElement().getElementsByTagName(goog.dom.TagName.LI)[2];
<a name="line264"></a>    helper.select(li.firstChild, 0);
<a name="line265"></a>    goog.testing.events.fireKeySequence(field1.getElement(),
<a name="line266"></a>                                        goog.events.KeyCodes.ENTER);
<a name="line267"></a>    helper.assertHtmlMatches(
<a name="line268"></a>        &#39;&lt;ul&gt;&#39; +
<a name="line269"></a>        &#39;&lt;li&gt;A&lt;/li&gt;&#39; +
<a name="line270"></a>        &#39;&lt;ul style=&quot;font-weight: bold&quot;&gt;&lt;li&gt;1&lt;/li&gt;&lt;/ul&gt;&#39; +
<a name="line271"></a>        &#39;&lt;li&gt;&amp;nbsp;&lt;/li&gt;&#39; +
<a name="line272"></a>        &#39;&lt;ul style=&quot;font-weight: bold&quot;&gt;&lt;li&gt;2&lt;/li&gt;&lt;/ul&gt;&#39; +
<a name="line273"></a>        &#39;&lt;li&gt;B&lt;/li&gt;&#39; +
<a name="line274"></a>        &#39;&lt;/ul&gt;&#39;);
<a name="line275"></a>  }
<a name="line276"></a>}
<a name="line277"></a>
<a name="line278"></a>
<a name="line279"></a>function testEnterInEmptyListItemAtBeginningOfSublist() {
<a name="line280"></a>  if (goog.userAgent.GECKO) {
<a name="line281"></a>    field1.setHtml(false,
<a name="line282"></a>        &#39;&lt;ul&gt;&#39; +
<a name="line283"></a>        &#39;&lt;li&gt;A&lt;/li&gt;&#39; +
<a name="line284"></a>        &#39;&lt;ul style=&quot;font-weight: bold&quot;&gt;&#39; +
<a name="line285"></a>        &#39;&lt;li&gt;&amp;nbsp;&lt;/li&gt;&#39; +
<a name="line286"></a>        &#39;&lt;li&gt;1&lt;/li&gt;&#39; +
<a name="line287"></a>        &#39;&lt;li&gt;2&lt;/li&gt;&#39; +
<a name="line288"></a>        &#39;&lt;/ul&gt;&#39; +
<a name="line289"></a>        &#39;&lt;li&gt;B&lt;/li&gt;&#39; +
<a name="line290"></a>        &#39;&lt;/ul&gt;&#39;);
<a name="line291"></a>    var helper = new goog.testing.editor.TestHelper(field1.getElement());
<a name="line292"></a>    var li = field1.getElement().getElementsByTagName(goog.dom.TagName.LI)[1];
<a name="line293"></a>    helper.select(li.firstChild, 0);
<a name="line294"></a>    goog.testing.events.fireKeySequence(field1.getElement(),
<a name="line295"></a>                                        goog.events.KeyCodes.ENTER);
<a name="line296"></a>    helper.assertHtmlMatches(
<a name="line297"></a>        &#39;&lt;ul&gt;&#39; +
<a name="line298"></a>        &#39;&lt;li&gt;A&lt;/li&gt;&#39; +
<a name="line299"></a>        &#39;&lt;li&gt;&amp;nbsp;&lt;/li&gt;&#39; +
<a name="line300"></a>        &#39;&lt;ul style=&quot;font-weight: bold&quot;&gt;&lt;li&gt;1&lt;/li&gt;&lt;li&gt;2&lt;/li&gt;&lt;/ul&gt;&#39; +
<a name="line301"></a>        &#39;&lt;li&gt;B&lt;/li&gt;&#39; +
<a name="line302"></a>        &#39;&lt;/ul&gt;&#39;);
<a name="line303"></a>  }
<a name="line304"></a>}
<a name="line305"></a>
<a name="line306"></a>
<a name="line307"></a>function testEnterInEmptyListItemAtEndOfSublist() {
<a name="line308"></a>  if (goog.userAgent.GECKO) {
<a name="line309"></a>    field1.setHtml(false,
<a name="line310"></a>        &#39;&lt;ul&gt;&#39; +
<a name="line311"></a>        &#39;&lt;li&gt;A&lt;/li&gt;&#39; +
<a name="line312"></a>        &#39;&lt;ul style=&quot;font-weight: bold&quot;&gt;&#39; +
<a name="line313"></a>        &#39;&lt;li&gt;1&lt;/li&gt;&#39; +
<a name="line314"></a>        &#39;&lt;li&gt;2&lt;/li&gt;&#39; +
<a name="line315"></a>        &#39;&lt;li&gt;&amp;nbsp;&lt;/li&gt;&#39; +
<a name="line316"></a>        &#39;&lt;/ul&gt;&#39; +
<a name="line317"></a>        &#39;&lt;li&gt;B&lt;/li&gt;&#39; +
<a name="line318"></a>        &#39;&lt;/ul&gt;&#39;);
<a name="line319"></a>    var helper = new goog.testing.editor.TestHelper(field1.getElement());
<a name="line320"></a>    var li = field1.getElement().getElementsByTagName(goog.dom.TagName.LI)[3];
<a name="line321"></a>    helper.select(li.firstChild, 0);
<a name="line322"></a>    goog.testing.events.fireKeySequence(field1.getElement(),
<a name="line323"></a>                                        goog.events.KeyCodes.ENTER);
<a name="line324"></a>    helper.assertHtmlMatches(
<a name="line325"></a>        &#39;&lt;ul&gt;&#39; +
<a name="line326"></a>        &#39;&lt;li&gt;A&lt;/li&gt;&#39; +
<a name="line327"></a>        &#39;&lt;ul style=&quot;font-weight: bold&quot;&gt;&lt;li&gt;1&lt;/li&gt;&lt;li&gt;2&lt;/li&gt;&lt;/ul&gt;&#39; +
<a name="line328"></a>        &#39;&lt;li&gt;&amp;nbsp;&lt;/li&gt;&#39; +
<a name="line329"></a>        &#39;&lt;li&gt;B&lt;/li&gt;&#39; +
<a name="line330"></a>        &#39;&lt;/ul&gt;&#39;);
<a name="line331"></a>  }
<a name="line332"></a>}
<a name="line333"></a>
<a name="line334"></a>
<a name="line335"></a>function testPrepareContentForPOnEnter() {
<a name="line336"></a>  assertPreparedContents(&#39;hi&#39;, &#39;hi&#39;);
<a name="line337"></a>  assertPreparedContents(
<a name="line338"></a>      goog.editor.BrowserFeature.COLLAPSES_EMPTY_NODES ? &#39;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&#39; : &#39;&#39;,
<a name="line339"></a>      &#39;   &#39;);
<a name="line340"></a>}
<a name="line341"></a>
<a name="line342"></a>
<a name="line343"></a>function testPrepareContentForDivOnEnter() {
<a name="line344"></a>  assertPreparedContents(&#39;hi&#39;, &#39;hi&#39;, goog.dom.TagName.DIV);
<a name="line345"></a>  assertPreparedContents(
<a name="line346"></a>      goog.editor.BrowserFeature.COLLAPSES_EMPTY_NODES ? &#39;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&#39; : &#39;&#39;,
<a name="line347"></a>      &#39;   &#39;,
<a name="line348"></a>      goog.dom.TagName.DIV);
<a name="line349"></a>}
<a name="line350"></a>
<a name="line351"></a>
<a name="line352"></a>/**
<a name="line353"></a> * Assert that the prepared contents matches the expected.
<a name="line354"></a> */
<a name="line355"></a>function assertPreparedContents(expected, original, opt_tag) {
<a name="line356"></a>  var field = makeField(&#39;field1&#39;, opt_tag);
<a name="line357"></a>  field.makeEditable();
<a name="line358"></a>  assertEquals(expected,
<a name="line359"></a>      field.reduceOp_(
<a name="line360"></a>          goog.editor.Plugin.Op.PREPARE_CONTENTS_HTML, original));
<a name="line361"></a>}
<a name="line362"></a>
<a name="line363"></a>
<a name="line364"></a>/**
<a name="line365"></a> * Selects the node at the given id, and simulates an ENTER keypress.
<a name="line366"></a> * @param {googe.editor.Field} field The field with the node.
<a name="line367"></a> * @param {string} id A DOM id.
<a name="line368"></a> * @return {boolean} Whether preventDefault was called on the event.
<a name="line369"></a> */
<a name="line370"></a>function selectNodeAndHitEnter(field, id) {
<a name="line371"></a>  var cursor = field.getEditableDomHelper().getElement(id);
<a name="line372"></a>  goog.dom.Range.createFromNodeContents(cursor).select();
<a name="line373"></a>  return goog.testing.events.fireKeySequence(
<a name="line374"></a>      cursor, goog.events.KeyCodes.ENTER);
<a name="line375"></a>}
<a name="line376"></a>
<a name="line377"></a>
<a name="line378"></a>/**
<a name="line379"></a> * Creates a field with only the enter handler plugged in, for testing.
<a name="line380"></a> * @param {string} id A DOM id.
<a name="line381"></a> * @param {boolean=} opt_tag The block tag to use.  Defaults to P.
<a name="line382"></a> * @return {goog.editor.Field} A field.
<a name="line383"></a> */
<a name="line384"></a>function makeField(id, opt_tag) {
<a name="line385"></a>  var field = new goog.editor.Field(id);
<a name="line386"></a>  field.registerPlugin(
<a name="line387"></a>      new goog.editor.plugins.TagOnEnterHandler(opt_tag || goog.dom.TagName.P));
<a name="line388"></a>  return field;
<a name="line389"></a>}
<a name="line390"></a>
<a name="line391"></a>
<a name="line392"></a>/**
<a name="line393"></a> * Runs a test for splitting the dom.
<a name="line394"></a> * @param {number} offset Index into the text node to split.
<a name="line395"></a> * @param {string} firstHalfString What the html of the first half of the DOM
<a name="line396"></a> *     should be.
<a name="line397"></a> * @param {string} secondHalfString What the html of the 2nd half of the DOM
<a name="line398"></a> *     should be.
<a name="line399"></a> * @param {boolean} isAppend True if the second half should be appended to the
<a name="line400"></a> *     DOM.
<a name="line401"></a> * @param {boolean=} opt_goToRoot True if the root argument for splitDom should
<a name="line402"></a> *     be excluded.
<a name="line403"></a> */
<a name="line404"></a>function helpTestSplit_(offset, firstHalfString, secondHalfString, isAppend,
<a name="line405"></a>    opt_goToBody) {
<a name="line406"></a>  var node = document.createElement(&#39;div&#39;);
<a name="line407"></a>  node.innerHTML = &#39;&lt;b&gt;begin bold&lt;i&gt;italic&lt;/i&gt;end bold&lt;/b&gt;&#39;;
<a name="line408"></a>  document.body.appendChild(node);
<a name="line409"></a>
<a name="line410"></a>  var italic = node.getElementsByTagName(&#39;i&#39;)[0].firstChild;
<a name="line411"></a>
<a name="line412"></a>  var splitFn = isAppend ?
<a name="line413"></a>      goog.editor.plugins.TagOnEnterHandler.splitDomAndAppend_ :
<a name="line414"></a>      goog.editor.plugins.TagOnEnterHandler.splitDom_;
<a name="line415"></a>  var secondHalf = splitFn(italic, offset, opt_goToBody ? undefined : node);
<a name="line416"></a>
<a name="line417"></a>  if (opt_goToBody) {
<a name="line418"></a>    secondHalfString = &#39;&lt;div&gt;&#39; + secondHalfString + &#39;&lt;/div&gt;&#39;;
<a name="line419"></a>  }
<a name="line420"></a>
<a name="line421"></a>  assertEquals(&#39;original node should have first half of the html&#39;,
<a name="line422"></a>               firstHalfString,
<a name="line423"></a>               node.innerHTML.toLowerCase().
<a name="line424"></a>      replace(goog.string.Unicode.NBSP, &#39;&amp;nbsp;&#39;));
<a name="line425"></a>  assertEquals(&#39;new node should have second half of the html&#39;,
<a name="line426"></a>               secondHalfString,
<a name="line427"></a>               secondHalf.innerHTML.toLowerCase().
<a name="line428"></a>                   replace(goog.string.Unicode.NBSP, &#39;&amp;nbsp;&#39;));
<a name="line429"></a>
<a name="line430"></a>  if (isAppend) {
<a name="line431"></a>    assertTrue(&#39;second half of dom should be the original node\&#39;s next&#39; +
<a name="line432"></a>               &#39;sibling&#39;, node.nextSibling == secondHalf);
<a name="line433"></a>    goog.dom.removeNode(secondHalf);
<a name="line434"></a>  }
<a name="line435"></a>
<a name="line436"></a>  goog.dom.removeNode(node);
<a name="line437"></a>}
<a name="line438"></a>
<a name="line439"></a>
<a name="line440"></a>/**
<a name="line441"></a> * Runs different cases of splitting the DOM.
<a name="line442"></a> * @param {function(number, string, string)} testFn Function that takes an
<a name="line443"></a> *     offset, firstHalfString and secondHalfString as parameters.
<a name="line444"></a> */
<a name="line445"></a>function splitDomCases_(testFn) {
<a name="line446"></a>  testFn(3, &#39;&lt;b&gt;begin bold&lt;i&gt;ita&lt;/i&gt;&lt;/b&gt;&#39;, &#39;&lt;b&gt;&lt;i&gt;lic&lt;/i&gt;end bold&lt;/b&gt;&#39;);
<a name="line447"></a>  testFn(0, &#39;&lt;b&gt;begin bold&lt;i&gt;&amp;nbsp;&lt;/i&gt;&lt;/b&gt;&#39;, &#39;&lt;b&gt;&lt;i&gt;italic&lt;/i&gt;end bold&lt;/b&gt;&#39;);
<a name="line448"></a>  testFn(6, &#39;&lt;b&gt;begin bold&lt;i&gt;italic&lt;/i&gt;&lt;/b&gt;&#39;, &#39;&lt;b&gt;&lt;i&gt;&amp;nbsp;&lt;/i&gt;end bold&lt;/b&gt;&#39;);
<a name="line449"></a>}
<a name="line450"></a>
<a name="line451"></a>
<a name="line452"></a>function testSplitDom() {
<a name="line453"></a>  splitDomCases_(function(offset, firstHalfString, secondHalfString) {
<a name="line454"></a>    helpTestSplit_(offset, firstHalfString, secondHalfString, false, true);
<a name="line455"></a>    helpTestSplit_(offset, firstHalfString, secondHalfString, false, false);
<a name="line456"></a>  });
<a name="line457"></a>}
<a name="line458"></a>
<a name="line459"></a>
<a name="line460"></a>function testSplitDomAndAppend() {
<a name="line461"></a>  splitDomCases_(function(offset, firstHalfString, secondHalfString) {
<a name="line462"></a>    helpTestSplit_(offset, firstHalfString, secondHalfString, true, false);
<a name="line463"></a>  });
<a name="line464"></a>}
<a name="line465"></a>
<a name="line466"></a>
<a name="line467"></a>function testSplitDomAtElement() {
<a name="line468"></a>  var node = document.createElement(&#39;div&#39;);
<a name="line469"></a>  node.innerHTML = &#39;&lt;div&gt;abc&lt;br&gt;def&lt;/div&gt;&#39;;
<a name="line470"></a>  document.body.appendChild(node);
<a name="line471"></a>
<a name="line472"></a>  goog.editor.plugins.TagOnEnterHandler.splitDomAndAppend_(node.firstChild, 1,
<a name="line473"></a>      node.firstChild);
<a name="line474"></a>
<a name="line475"></a>  goog.testing.dom.assertHtmlContentsMatch(&#39;&lt;div&gt;abc&lt;/div&gt;&lt;div&gt;&lt;br&gt;def&lt;/div&gt;&#39;,
<a name="line476"></a>      node);
<a name="line477"></a>
<a name="line478"></a>  goog.dom.removeNode(node);
<a name="line479"></a>}
<a name="line480"></a>
<a name="line481"></a>
<a name="line482"></a>function testSplitDomAtElementStart() {
<a name="line483"></a>  var node = document.createElement(&#39;div&#39;);
<a name="line484"></a>  node.innerHTML = &#39;&lt;div&gt;abc&lt;br&gt;def&lt;/div&gt;&#39;;
<a name="line485"></a>  document.body.appendChild(node);
<a name="line486"></a>
<a name="line487"></a>  goog.editor.plugins.TagOnEnterHandler.splitDomAndAppend_(node.firstChild, 0,
<a name="line488"></a>      node.firstChild);
<a name="line489"></a>
<a name="line490"></a>  goog.testing.dom.assertHtmlContentsMatch(&#39;&lt;div&gt;&lt;/div&gt;&lt;div&gt;abc&lt;br&gt;def&lt;/div&gt;&#39;,
<a name="line491"></a>      node);
<a name="line492"></a>
<a name="line493"></a>  goog.dom.removeNode(node);
<a name="line494"></a>}
<a name="line495"></a>
<a name="line496"></a>
<a name="line497"></a>function testSplitDomAtChildlessElement() {
<a name="line498"></a>  var node = document.createElement(&#39;div&#39;);
<a name="line499"></a>  node.innerHTML = &#39;&lt;div&gt;abc&lt;br&gt;def&lt;/div&gt;&#39;;
<a name="line500"></a>  document.body.appendChild(node);
<a name="line501"></a>
<a name="line502"></a>  var br = node.getElementsByTagName(goog.dom.TagName.BR)[0];
<a name="line503"></a>  goog.editor.plugins.TagOnEnterHandler.splitDomAndAppend_(
<a name="line504"></a>      br, 0, node.firstChild);
<a name="line505"></a>
<a name="line506"></a>  goog.testing.dom.assertHtmlContentsMatch(&#39;&lt;div&gt;abc&lt;/div&gt;&lt;div&gt;&lt;br&gt;def&lt;/div&gt;&#39;,
<a name="line507"></a>      node);
<a name="line508"></a>
<a name="line509"></a>  goog.dom.removeNode(node);
<a name="line510"></a>}
<a name="line511"></a>
<a name="line512"></a>function testReplaceWhiteSpaceWithNbsp() {
<a name="line513"></a>  var node = document.createElement(&#39;div&#39;);
<a name="line514"></a>  var textNode = document.createTextNode(&#39;&#39;);
<a name="line515"></a>  node.appendChild(textNode);
<a name="line516"></a>
<a name="line517"></a>  textNode.nodeValue = &#39; test &#39;;
<a name="line518"></a>  goog.editor.plugins.TagOnEnterHandler.replaceWhiteSpaceWithNbsp_(
<a name="line519"></a>      node.firstChild, true, false);
<a name="line520"></a>  assertHTMLEquals(&#39;&amp;nbsp;test &#39;, node.innerHTML);
<a name="line521"></a>
<a name="line522"></a>  textNode.nodeValue = &#39;  test &#39;;
<a name="line523"></a>  goog.editor.plugins.TagOnEnterHandler.replaceWhiteSpaceWithNbsp_(
<a name="line524"></a>      node.firstChild, true, false);
<a name="line525"></a>  assertHTMLEquals(&#39;&amp;nbsp;test &#39;, node.innerHTML);
<a name="line526"></a>
<a name="line527"></a>  textNode.nodeValue = &#39; test &#39;;
<a name="line528"></a>  goog.editor.plugins.TagOnEnterHandler.replaceWhiteSpaceWithNbsp_(
<a name="line529"></a>      node.firstChild, false, false);
<a name="line530"></a>  assertHTMLEquals(&#39; test&amp;nbsp;&#39;, node.innerHTML);
<a name="line531"></a>
<a name="line532"></a>  textNode.nodeValue = &#39; test  &#39;;
<a name="line533"></a>  goog.editor.plugins.TagOnEnterHandler.replaceWhiteSpaceWithNbsp_(
<a name="line534"></a>      node.firstChild, false, false);
<a name="line535"></a>  assertHTMLEquals(&#39; test&amp;nbsp;&#39;, node.innerHTML);
<a name="line536"></a>
<a name="line537"></a>  textNode.nodeValue = &#39;&#39;;
<a name="line538"></a>  goog.editor.plugins.TagOnEnterHandler.replaceWhiteSpaceWithNbsp_(
<a name="line539"></a>      node.firstChild, false, false);
<a name="line540"></a>  assertHTMLEquals(&#39;&amp;nbsp;&#39;, node.innerHTML);
<a name="line541"></a>
<a name="line542"></a>  textNode.nodeValue = &#39;&#39;;
<a name="line543"></a>  goog.editor.plugins.TagOnEnterHandler.replaceWhiteSpaceWithNbsp_(
<a name="line544"></a>      node.firstChild, false, true);
<a name="line545"></a>  assertHTMLEquals(&#39;&#39;, node.innerHTML);
<a name="line546"></a>}
</pre>


</body>
</html>
